package cn.doctorpda.conference.update;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;

import cn.doctorpda.conference.R;


/**
 * 更新版本的dialog
 * create by jiangxiufeng
 */
public class BaiyyyUpdateDialogFragment extends DialogFragment implements OnClickListener{


    private TextView uploadTxt;
    private TextView cancleTxt;
    private TextView contentTxt;

    private BaiyyyUpdateInfo updateInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        updateInfo= (BaiyyyUpdateInfo) getArguments().getSerializable("updateInfo");
        View inflate = inflater.inflate(R.layout.baiyyy_fragment_dialog_update, null);
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(false);
        uploadTxt=(TextView)inflate.findViewById(R.id.txt_upload);
        contentTxt=(TextView)inflate.findViewById(R.id.content_txt);
        cancleTxt=(TextView)inflate.findViewById(R.id.txt_cancle);


        StringBuffer content=new StringBuffer();
        content.append("最新版本:&nbsp;"+"<font color='red'>"+updateInfo.getVersion()+"</font>");
        content.append("<br/><br/>");
        content.append(!TextUtils.isEmpty(updateInfo.getChangelog())?"更新内容:<br/>"+updateInfo.getChangelog()+"<br/>":"");
        contentTxt.setText(Html.fromHtml(content.toString()));

        uploadTxt.setOnClickListener(this);
        cancleTxt.setOnClickListener(this);
        return inflate;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.txt_upload:

                if (updateInfo != null && !TextUtils.isEmpty(updateInfo.getFilepath())) {
                    final ProgressDialog dialog = new ProgressDialog(getActivity());
                    dialog.setCanceledOnTouchOutside(false);
                    dialog.setMessage("正在下载....");
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.show();
                    final String installUrl = BaiyyyUpdateConfig.dirName + "/" + BaiyyyUpdateConfig.fileName + updateInfo.getVersion() + ".apk";
                    File file = new File(installUrl);
                    if (file.exists()){
                        if (file.exists()) {
                            Intent install = new Intent();
                            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            install.setAction(Intent.ACTION_VIEW);
                            install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                            startActivity(install);
                            dialog.dismiss();
                        }
                        return;
                    }

                    HttpUtils httpUtil=new HttpUtils();
                    httpUtil.download(updateInfo.getFilepath(), installUrl, new RequestCallBack() {
                        @Override
                        public void onSuccess(ResponseInfo responseInfo) {
                            dialog.dismiss();
                            File file = new File(installUrl);
                            if (file.exists()) {
                                Intent install = new Intent();
                                install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                install.setAction(Intent.ACTION_VIEW);
                                install.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                                startActivity(install);
                            }
                        }

                        @Override
                        public void onFailure(HttpException e, String s) {
                            dialog.dismiss();
                        }

                        @Override
                        public void onLoading(long total, long current, boolean isUploading) {
                            dialog.setMax((int) total);
                            dialog.setProgress((int) current);
                            super.onLoading(total, current, isUploading);
                        }
                    });
                }
                break;
            case R.id.txt_cancle:
                dismiss();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
